import math
import random

import Config
from tools import ActionUtil

"""
  预设移动策略：定义“移动距离”和“移动时间”的映射
  get_xmove_time
  get_ymove_time
"""


def move_random_far(position, location, seconds):
    """
    根据角色计算，向窗口中更远处移动
    position = {"left": 0, "top": 0, "width": 1000, "height": 570}
    """
    position_dic = dict(position)
    move_x = 0
    move_y = 0

    x_half = position_dic["width"] / 2
    if location[0] < x_half:
        # 站位偏左，向右移动
        move_x = "right"
    else:
        move_x = "left"

    y_half = position_dic["height"] / 2
    if location[1] < y_half:
        # 站位偏上，向下移动
        move_y = "down"
    else:
        move_y = "up"

    # x坐标离中线近, y坐标离中线远，则先向y轴移动
    if abs(location[0] - x_half) < abs(location[1] - y_half):
        print("*" * 10, f"random far y/x", "*" * 10)
        ActionUtil.press_delay(move_y, seconds)
        ActionUtil.press_delay(move_x, seconds)
    else:
        print("*" * 10, f"random far x/y", "*" * 10)
        ActionUtil.press_delay(move_x, seconds)
        ActionUtil.press_delay(move_y, seconds)

    return True


def move_random(seconds):
    value = random.randint(1, 4)
    if value == 1:
        if Config.move_log:
            print("*" * 10, f"random up {seconds}", "*" * 10)
        ActionUtil.press_delay("up", seconds)
    elif value == 2:
        if Config.move_log:
            print("*" * 10, f"random right {seconds}", "*" * 10)
        ActionUtil.press_delay("right", seconds)
    elif value == 3:
        if Config.move_log:
            print("*" * 10, f"random_down {seconds}", "*" * 10)
        ActionUtil.press_delay("down", seconds)
    else:
        if Config.move_log:
            print("*" * 10, f"random left {seconds}", "*" * 10)
        ActionUtil.press_delay("left", seconds)
    return True


def moveTo(role_loc, target_loc):
    if Config.move_log:
        print("*" * 10, f"move_{random.randint(1, 100)}", "*" * 10)
    if random.random() > 0.5:
        move_x(role_loc, target_loc)
        move_y(role_loc, target_loc)
    else:
        move_y(role_loc, target_loc)
        move_x(role_loc, target_loc)
    return True


def move_x(role_loc, target_loc):
    is_move = True
    if role_loc[0] < target_loc[0]:
        distance = target_loc[0] - role_loc[0]
        # 目标在右边，向右移动
        if Config.move_log:
            print(f"向右移动: {distance}")
        ActionUtil.press_delay("right", get_xmove_time(distance))

    elif role_loc[0] > target_loc[0]:
        distance = role_loc[0] - target_loc[0]
        # 目标在左边，向左移动
        if Config.move_log:
            print(f"向左移动: {distance}")
        ActionUtil.press_delay("left", get_xmove_time(distance))
    else:
        is_move = False

    return is_move


def move_y(role_loc, target_loc):
    is_move = True

    if role_loc[1] < target_loc[1]:
        distance = target_loc[1] - role_loc[1]
        # 目标在下边，向下移动
        if Config.move_log:
            print(f"向下移动: {distance}")
        ActionUtil.press_delay("down", get_ymove_time(distance))

    elif role_loc[1] > target_loc[1]:
        distance = role_loc[1] - target_loc[1]
        # 目标在上边，向上移动
        if Config.move_log:
            print(f"向上移动: {distance}")
        ActionUtil.press_delay("up", get_ymove_time(distance))
    else:
        is_move = False

    return is_move


def get_xmove_time(x_distance):
    value = round(math.ceil(x_distance / 60) * 0.1, 1)
    if Config.move_log:
        print(f"move_time:{value}")
    return value


def get_ymove_time(y_distance):
    value = round(math.ceil(y_distance / 50) * 0.1, 1)
    if Config.move_log:
        print(f"move_time:{value}")
    return value


def is_need_enter(rol_loc, target_loc, distance=100):
    """
    判断是否在门的附近
    """
    a_x = abs(rol_loc[0] - target_loc[0])
    a_y = abs(rol_loc[1] - target_loc[1])
    if a_x < distance and a_y < distance:
        return True
    else:
        return False
